/* Copyright 2011 Elektrobit Wireless Communications Oy (EB)


  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


 *All rights reserved 
 *Version:Elektrobit_Qt_1.0.0_20110305 */


#include "routerservice.h"
#include <servicemanager.h>
#include <QApplication>
#include <appcommon.h>
RouterService::RouterService(QObject *parent) : Service(parent) {
    this->mPage = NULL;
    mPages.clear();
}

int RouterService::start() {
    return 0;
}

//需要重构,目前先写出流程
void RouterService::redirectTo(const QString &nodeKey, const QHash<QString, QString> *args) {
    Page *currentPage = this->mPage;
    this->mPage = NULL;
    Page *page = SERVICE(PageService)->getPage(nodeKey);
    SERVICE(BackgroundService)->getBackgroundWidget()->showMessage(QString::fromUtf8("加载中..."));
    QApplication::processEvents();
    if(page == NULL) {
        this->mPage = currentPage;
        SERVICE(BackgroundService)->getBackgroundWidget()->hideMessage();
        return;
    }
    if(currentPage != NULL) {
        currentPage->hide();
        QApplication::processEvents();
        this->bindParentProperties(page, currentPage, nodeKey);
    }
    if(args != NULL) {
        QHashIterator<QString, QString> i(*args);
        while (i.hasNext()) {
            i.next();
            page->setProperty(i.key().toUtf8().data(), i.value().toUtf8().data());
        }
        delete args;
    }
    if(page->init()) {
        this->mPage = currentPage;
        this->mPage->show();
        SERVICE(BackgroundService)->getBackgroundWidget()->hideMessage();
        return;
    }
    if(page->beforeShow()) {
        this->mPage = currentPage;
        page->uninit();
        this->mPage->show();
        SERVICE(BackgroundService)->getBackgroundWidget()->hideMessage();
        return;
    }
    page->show();
    SERVICE(BackgroundService)->getBackgroundWidget()->hideMessage();
    QApplication::processEvents();
    if(page->afterShow()) {
        this->mPage = currentPage;
        page->hide();
        page->uninit();
        if(currentPage != NULL) {
            currentPage->show();
        }
        return;
    }
    QApplication::processEvents();
    this->mPage = page;
    if(currentPage != NULL) {
//        currentPage->uninit();
        mPages.push(currentPage);
    }
}

void RouterService::goBack(const QHash<QString, QString> *args) {
//    if(this->mPage != NULL) {
//        this->redirectTo(mPage->property("parent_node").toString(), args);
//    }
    if(mPages.size() == 0) {
        return;
    }
    Page *currentPage = this->mPage;
    if(currentPage) {
        currentPage->hide();
        currentPage->uninit();
//        currentPage->deleteLater()
    }
    Page * page = mPages.pop();
    page->show();
    this->mPage = page;
}


int RouterService::keyEvent(int key) {
    if(!this->mPage) {
        return 0;
    }
    if(!this->mPage->isPageViewShow()) {
        return 0;
    }
    return this->mPage->onClick(key);
}

int RouterService::mouseEvent(QMouseEvent *event) {
    APPINFO;
    if(this->mPage) {
        this->mPage->mouseEvent(event);
    }
}

void RouterService::bindParentProperties(Page *page, Page *parentPage, const QString &nodeKey) {
    QHash<QString, QString> map = SERVICE(PageService)->getParentProperties(nodeKey);
    QHashIterator<QString, QString> i(map);
    while (i.hasNext()) {
        i.next();
        page->setProperty(i.value().toUtf8().data(), parentPage->property(i.key().toUtf8().data()));
    }
}
